package com.health.work.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class WebUtils {
    
    //
    private static final String SERVER_NAMESPACE = "http://cnhealth.cn/";
    private static final String SOAP_ACTION = "http://cnhealth.cn/";
    private static final String SERVER_URL = "http://mc.cnhealth.cn:81/TestAndroidService/Service.asmx";
    // 方法名称
    private static final String FUC_REGISTER = "Registe";
    private static final String FUC_LOGIN = "Login";
    private static final String FUC_CHANGEPASSWORD = "ChangePassword";
    private static final String FUC_GETEHR = "GetEHR";
    private static final String FUC_RESETPASSWORD = "ResetPassword";
    private static final String FUC_UPDATEMYSELF = "UpdateMyself";
    private static final String FUC_FEEDBACKMSG ="FeedBackMsg";
    private static final String FUC_UPLOADPICTURE ="UploadPictureBytes";
    private static final String FUC_EXISTNEWLYEHR ="ExisitNewlyEHR";
    private static final String FUC_GETSHOWLINK ="GetShowLink";
    private static final String FUC_GETITEMS = "GetItems";
    
    
    // 图片数量
    private static int picNum = 0;
    // 图片链接数组
    private static String [] PICURL;
    // 图片对应超链接数组
    private static String [] LinkURL;
    // 最近一次检查日期
    private static String LASTCHECKDATE  = "1900-01-01";
    // 检查日期列表
    private static ArrayList LIST_CHECKDATE;
    
 // 体检项目
    private static String [] EHRItems;
    
    public static String[] getEHRItems() {
        return EHRItems;
    }

    public static void setEHRItems(String[] eHRItems) {
        EHRItems = eHRItems;
    }
    
    public static int getPicNum() {
        return picNum;
    }

    public static void setPicNum(int picNum) {
        WebUtils.picNum = picNum;
    }

    public static String[] getPICURL() {
        return PICURL;
    }

    public static void setPICURL(String[] pICURL) {
        PICURL = pICURL;
    }

    public static String[] getLinkURL() {
        return LinkURL;
    }

    public static void setLinkURL(String[] linkURL) {
        LinkURL = linkURL;
    }


    public static String getLASTCHECKDATE() {
        return LASTCHECKDATE;
    }

    public static void setLASTCHECKDATE(String lASTCHECKDATE) {
        LASTCHECKDATE = lASTCHECKDATE;
    }

    
    public static ArrayList getLIST_CHECKDATE() {
        return LIST_CHECKDATE;
    }

    public static void setLIST_CHECKDATE(ArrayList lIST_CHECKDATE) {
        LIST_CHECKDATE = lIST_CHECKDATE;
    }

    /** 注册账户
    * 
    * @param IdCardNo
    * @param Name
    * @param Phone
    * @param UserName
    * @param Password
    * @return
    */
   public static String Register(String IdCardNo, String Name, String Phone,
           String UserName, String Password) {
       String str_result = null;
       SoapObject request = new SoapObject(SERVER_NAMESPACE, FUC_REGISTER);
       Log.d("guojie", "new SoapObject(SERVER_NAMESPACE,FUC_REGISTER)");
       request.addProperty("IdCardNo",IdCardNo);
       request.addProperty("Name",Name);
       request.addProperty("Phone",Phone);
       request.addProperty("UserName",UserName);
       request.addProperty("Password",Password);
       Log.d("guojie"," idcardno =="+IdCardNo+"  size ===="+IdCardNo.length());
       Log.d("guojie"," name =="+Name+"  size ===="+Name.length());
       Log.d("guojie"," phone =="+Phone+"  size ===="+Phone.length());
       Log.d("guojie"," username =="+UserName+"  size ===="+UserName.length());
       Log.d("guojie"," pwd =="+Password+"  size ===="+Password.length());
       
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
               SoapEnvelope.VER11);
       envelope.bodyOut = request;
       envelope.dotNet = false;
       envelope.setOutputSoapObject(request);
       
       
       AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
       Log.d("guojie", "new AndroidHttpTransport(SERVER_URL)");
       ht.debug = true;
      
       Log.d("guojie", "envelope.setOutputSoapObject(request);");
       try {
           ht.call(SOAP_ACTION+FUC_REGISTER, envelope);
           Log.d("guojie", "ht.call(null, envelope);");
           SoapObject result = (SoapObject) envelope.getResponse();
           Log.d("guojie", "(SoapObject) envelope.getResponse()");
           str_result = result.getProperty("IsSuccess").toString() + "@";
           Log.d("guojie", "str_result == " + str_result);
           if(str_result.contains("true")) {
               str_result = str_result+"操作成功";
           } else {
               str_result = str_result
                       +  result.getProperty("ErrorMessage").toString();
           }
           Log.d("guojie", "str_result == " + str_result);
       } catch (IOException e) {
           Log.e("guojie", "IOException error === " + e.toString());
           e.printStackTrace();
       } catch (XmlPullParserException e) {
           Log.e("guojie", "XmlPullParserException error === " + e.toString());
           e.printStackTrace();
       } catch(Exception e) {
           Log.e("guojie", "Exception error === " + e.toString());
           e.printStackTrace();
       }

       return str_result;
   }

   /**
    * 登录
    * 
    * @param Identity
    * @param Password
    * @return
    */
   public static String Login(String Identity, String Password) {
       String str_result = null;
       SoapObject request = new SoapObject(SERVER_NAMESPACE, FUC_LOGIN);
       request.addProperty("Identity", Identity);
       request.addProperty("Password", Password);

       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
               SoapEnvelope.VER11);
       envelope.bodyOut = request;
       envelope.dotNet = false;
   
       envelope.setOutputSoapObject(request);
       AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
       ht.debug = true;
       try {
           ht.call(SOAP_ACTION+FUC_LOGIN, envelope);
           SoapObject result = (SoapObject) envelope.getResponse();
           str_result = result.getProperty("IsSuccess").toString() + "@";
           Log.d("guojie", "str_result == " + str_result);
           if(str_result.contains("true")) {
               str_result = str_result+"操作成功";
           } else {
               str_result = str_result
                       +  result.getProperty("ErrorMessage").toString();
           }
           Log.d("guojie", "str_result == " + str_result);
       } catch (IOException e) {
           e.printStackTrace();
       } catch (XmlPullParserException e) {
           e.printStackTrace();
       } catch (Exception e) {
           e.printStackTrace();
       } 
       return str_result;
   }

   /**
    * 修改密码
    * 
    * @param Identity
    * @param OldPassword
    * @param NewPassword
    * @return
    */
   public static String ChangePassword(String Identity, String OldPassword,
           String NewPassword) {
       String str_result = null;
       SoapObject request = new SoapObject(SERVER_NAMESPACE,
               FUC_CHANGEPASSWORD);
       request.addProperty("Identity", Identity);
       request.addProperty("OldPassword", OldPassword);
       request.addProperty("NewPassword", NewPassword);
  
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
               SoapEnvelope.VER11);
       envelope.bodyOut = request;
       envelope.dotNet = false;
       envelope.setOutputSoapObject(request);
       
       AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
       ht.debug = true;
       try {
           ht.call(SOAP_ACTION+FUC_CHANGEPASSWORD, envelope);
           SoapObject result = (SoapObject) envelope.getResponse();
           str_result = result.getProperty("IsSuccess").toString() + "@";
           if(str_result.contains("true")) {
               str_result = str_result+"操作成功";
           } else {
               str_result = str_result
                       +  result.getProperty("ErrorMessage").toString();
           }
       } catch (IOException e) {
       } catch (XmlPullParserException e) {
       } catch (Exception e) {
           e.printStackTrace();
       } 
       return str_result;
   }
   /**
    * 获取健康信息
    * @param Identity
    * @param Password
    * @return
    */
   public static HashMap<String, Object> GetEHR(String Identity, String Password) {

       LIST_CHECKDATE = new ArrayList();
       HashMap<String, Object> hmap = new HashMap<String, Object>();
       SoapObject request = new SoapObject(SERVER_NAMESPACE, FUC_GETEHR);
       request.addProperty("Identity", Identity);
       request.addProperty("Password", Password);
    
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
               SoapEnvelope.VER11);
       envelope.bodyOut = request;
       envelope.dotNet = false;
       envelope.setOutputSoapObject(request);
       
       AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
       ht.debug = true;
       try {
           ht.call(SOAP_ACTION+FUC_GETEHR, envelope);
           Log.d("guojie", "ht.call(null, envelope);");
//           SoapPrimitive  result = (SoapPrimitive) envelope.getResponse();
//           SoapObject  result = (SoapObject) envelope.getResponse();
           Object  result = (Object) envelope.getResponse();
           String str_res = result.toString();
           Log.d("guojie", "envelope.getResponse() =="+result.toString());
           JSONObject rsJson = new JSONObject(str_res);
           String isSuccess = rsJson.getString("IsSuccess");
           if(isSuccess.contains("false")) {
               return null;
           }
           String PersonNum = rsJson.getString("PersonNum");
           Log.d("guojie", "PersonNum==="+PersonNum);
           String Name = rsJson.getString("Name");
           Log.d("guojie", "Name==="+Name);
           String Sex = rsJson.getString("Sex");
           Log.d("guojie", "Sex==="+Sex);
           String Age = rsJson.getString("Age");
           Log.d("guojie", "Age==="+Age);
           String IdCardNo = rsJson.getString("IdCardNo");
           Log.d("guojie", "IdCardNo==="+IdCardNo);
           String Address =rsJson.getString("Address");
           Log.d("guojie", "Address==="+Address);
           
           HashMap<String,Object> EHRecords = new HashMap<String, Object>();
           JSONArray ehrRecords = rsJson.getJSONArray("EHRRecords");
           for(int j = 0;j<ehrRecords.length();j++) {
               HashMap<String,Object> EHRecord = new HashMap<String, Object>();
               JSONObject ehrRecord = ehrRecords.optJSONObject(j);
               String CheckSN = ehrRecord.getString("CheckSN");
               String CheckDate = ehrRecord.getString("CheckDate");
               // 设置最近体检日期
               LIST_CHECKDATE.add(CheckDate);
               LASTCHECKDATE = CompareDate(CheckDate);
               String HospName = ehrRecord.getString("HospName");
               JSONArray ehrDetails = ehrRecord.getJSONArray("EHRDetails");
               HashMap<String,Object> EHRMap = new HashMap<String, Object>();
               for(int i= 0;i<ehrDetails.length();i++) {
                   JSONObject ehrDetail = ehrDetails.optJSONObject(i);
                   HashMap<String, String> detail = new HashMap<String, String>();
                   String ItemSN = ehrDetail.getString("ItemSN");
                   String ItemCode = ehrDetail.getString("ItemCode");
                   // String ItemNameKey = ehrDetail.getString("ItemNameKey");
                   String ItemName = ehrDetail.getString("ItemName");
                   String ItemNameAs = ehrDetail.getString("ItemNameAs");
                   String ItemValueDesc = ehrDetail.getString("ItemValueDesc");
                   String ItemUnit =ehrDetail.getString("ItemUnit");
                   String ClinicalMeaning =ehrDetail.getString("ClinicalMeaning");
                   String NormalValue1 = ehrDetail.getString("NormalValue1");
                   String NormalValue2 = ehrDetail.getString("NormalValue2");
                   String Tips = ehrDetail.getString("Tips");
                   String OrderBy = ehrDetail.getString("OrderBy");
                   detail.put("ItemSN", ItemSN);
                   detail.put("ItemCode", ItemCode);
                   detail.put("ItemName", ItemName);
                   detail.put("ItemNameAs", ItemNameAs);
                   detail.put("ItemValueDesc", ItemValueDesc);
                   detail.put("ItemUnit", ItemUnit);
                   detail.put("ClinicalMeaning", ClinicalMeaning);
                   detail.put("NormalValue1", NormalValue1);
                   detail.put("NormalValue2", NormalValue2);
                   detail.put("Tips", Tips);
                   detail.put("OrderBy", OrderBy);
                   EHRMap.put(ItemName, detail);
               }
               Log.d("guojie", "EHRlist==="+EHRMap.toString());
               EHRecord.put("CheckSN", CheckSN);
               EHRecord.put("CheckDate", CheckDate);
               EHRecord.put("HospName", HospName);
               EHRecord.put("EHRlist", EHRMap);
               // add for test begin
//               String Date1 = "2011-12-06";
//               LIST_CHECKDATE.add(Date1);
//               LASTCHECKDATE = CompareDate(Date1);
//               String Date2 = "2011-12-07";
//               LIST_CHECKDATE.add(Date2);
//               LASTCHECKDATE = CompareDate(Date2);
//               String Date3 = "2011-12-02";
//               LIST_CHECKDATE.add(Date3);
//               LASTCHECKDATE = CompareDate(Date3);
//               EHRecords.put(Date1, EHRecord);
//               EHRecords.put(Date2, EHRecord);
//               EHRecords.put(Date3, EHRecord);
               // add for test end
               EHRecords.put(CheckDate, EHRecord);
           }
           
           

           hmap.put("PersonNum", PersonNum);
           hmap.put("Name", Name);
           hmap.put("Sex", Sex);
           hmap.put("Age", Age);
           
           hmap.put("IdCardNo", IdCardNo);
           hmap.put("Address", Address);
           hmap.put("EHRecords", EHRecords);
           Log.d("guojie", "PersonNum==="+hmap.get("PersonNum"));
           Log.d("guojie", "Name==="+hmap.get("Name"));
           Log.d("guojie", "Age==="+hmap.get("Age"));
           Log.d("guojie", "Sex==="+hmap.get("Sex"));
           Log.d("guojie", "IdCardNo==="+hmap.get("IdCardNo"));
           Log.d("guojie", "Address==="+hmap.get("Address"));
           HashMap<String,Object> alist = (HashMap<String,Object>)hmap.get(LIST_CHECKDATE.get(0));
//           Log.d("guojie", "CheckDate==="+alist.get("CheckDate"));
               HashMap<String, Object> details =
            		   (HashMap<String, Object>)
            		   alist.
            		   get("EHRlist");
               HashMap<String, String> detail = (HashMap<String, String>)details.get("体温");
               Log.d("guojie", "ItemSN==="+detail.get("ItemSN"));
               Log.d("guojie", "ItemCode==="+detail.get("ItemCode"));
               Log.d("guojie", "ItemName==="+detail.get("ItemName"));
               Log.d("guojie", "ItemNameAs==="+detail.get("ItemNameAs"));
               Log.d("guojie", "ItemValueDesc==="+detail.get("ItemValueDesc"));
               Log.d("guojie", "ItemUnit==="+detail.get("ItemUnit"));
               Log.d("guojie", "ClinicalMeaning==="+detail.get("ClinicalMeaning"));
               Log.d("guojie", "NormalValue1==="+detail.get("NormalValue1"));
               Log.d("guojie", "NormalValue2==="+detail.get("NormalValue2"));
               Log.d("guojie", "Tips==="+detail.get("Tips"));
               Log.d("guojie", "OrderBy==="+detail.get("OrderBy"));
               detail = (HashMap<String, String>)details.get("体重");
               if(detail!=null) {
                   Log.d("guojie", "detail==="+detail.toString());
               }
               Log.d("guojie", "@@@@@@@@@@@@@@Finish@@@@@@@@@@");
          

       } catch (IOException e) {
           e.printStackTrace();
       } catch (XmlPullParserException e) {
           e.printStackTrace();
       } catch (Exception e) {
           e.printStackTrace();
       } 
       
       sortDate();
       for(int i=0;i<LIST_CHECKDATE.size();i++) {
           Log.d("guojie", "Date === "+LIST_CHECKDATE.get(i));
       }
       return hmap;
   }
//  
   public static HashMap<String,Object> getEHTDetail(String str) {
       HashMap<String,Object> EHRMap = new HashMap<String,Object>();
       String [] CheckItemDetails = str.split("\\}");
       for(int i = 0;i<CheckItemDetails.length;i++) {
           String [] items = CheckItemDetails[i].split(";");
           if(items.length>10) {
               HashMap<String, String> detail = new HashMap<String, String>();
               String ItemSN = items[0].substring(items[0].indexOf("=")+1);
               String ItemCode = items[1].substring(items[1].indexOf("=")+1);
               String ItemNameKey = items[2].substring(0,items[2].indexOf("=")+1);
               String ItemName = items[2].substring(items[2].indexOf("=")+1);
               String ItemNameAs = items[3].substring(items[3].indexOf("=")+1);
               String ItemValueDesc = items[4].substring(items[4].indexOf("=")+1);
               String ItemUnit =items[5].substring(items[5].indexOf("=")+1);
               String ClinicalMeaning =items[6].substring(items[6].indexOf("=")+1);
               String NormalValue1 = items[7].substring(items[7].indexOf("=")+1);
               String NormalValue2 =items[8].substring(items[8].indexOf("=")+1);
               String Tips = items[9].substring(items[9].indexOf("=")+1);
               String OrderBy = items[10].substring(items[10].indexOf("=")+1);
               detail.put("ItemSN", ItemSN);
               detail.put("ItemCode", ItemCode);
               detail.put("ItemName", ItemName);
               detail.put("ItemNameAs", ItemNameAs);
               detail.put("ItemValueDesc", ItemValueDesc);
               detail.put("ItemUnit", ItemUnit);
               detail.put("ClinicalMeaning", ClinicalMeaning);
               detail.put("NormalValue1", NormalValue1);
               detail.put("NormalValue2", NormalValue2);
               detail.put("Tips", Tips);
               detail.put("OrderBy", OrderBy);
               EHRMap.put(ItemName, detail);
               Log.d("guojie", "ItemName  is "+ItemName);
               Log.d("guojie", "EHRMap.get(ItemName).toString()===="+EHRMap.get(ItemName).toString());
           }
       }
       return EHRMap;
   }
   /**
    * 重置密码
    * @param IdCardNo
    * @param Name
    * @param Phone
    * @param UserName
    * @param NewNewPassword
    * @return
    */
   public static String ResetPassword(String IdCardNo, String Name, String Phone,
           String UserName, String NewNewPassword) {
       String str_result = null;
       SoapObject request = new SoapObject(SERVER_NAMESPACE, FUC_RESETPASSWORD);
       request.addProperty("IdCardNo",IdCardNo);
       request.addProperty("Name",Name);
       request.addProperty("Phone",Phone);
       request.addProperty("UserName",UserName);
       request.addProperty("NewNewPassword",NewNewPassword);
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
               SoapEnvelope.VER11);
       envelope.bodyOut = request;
       envelope.dotNet = false;
       envelope.setOutputSoapObject(request);
       AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
       ht.debug = true;
       try {
           ht.call(SOAP_ACTION+FUC_RESETPASSWORD, envelope);
           Log.d("guojie", "ACTION+FUC_RESETPASSWORD");
           SoapObject result = (SoapObject) envelope.getResponse();
           Log.d("guojie", "envelope.getResponse() =="+result.toString());
           str_result = result.getProperty("IsSuccess").toString() + "@";
           if(str_result.contains("true")) {
               str_result = str_result+"操作成功";
           } else {
               str_result = str_result
                       +  result.getProperty("ErrorMessage").toString();
           }
       } catch (IOException e) {
           e.printStackTrace();
       } catch (XmlPullParserException e) {
           e.printStackTrace();
       } catch(Exception e) {
           e.printStackTrace();
       }

       return str_result;
   }
   /**
    * 反馈信息
    * @param msg
    * @return
    */
   public static String FeedBackMsg(String msg) {
       String str_result = null;
       SoapObject request = new SoapObject(SERVER_NAMESPACE, FUC_FEEDBACKMSG);
       request.addProperty("msg",msg );
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
               SoapEnvelope.VER11);
       envelope.bodyOut = request;
       envelope.dotNet = false;
       envelope.setOutputSoapObject(request);
       AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
       ht.debug = true;
       try {
           ht.call(SOAP_ACTION+FUC_FEEDBACKMSG, envelope);
           SoapObject result = (SoapObject) envelope.getResponse();
           str_result = result.getProperty("IsSuccess").toString() + "@";
           if(str_result.contains("true")) {
               str_result = str_result+"操作成功";
           } else {
               str_result = str_result
                       +  result.getProperty("ErrorMessage").toString();
           }
       } catch (IOException e) {
           e.printStackTrace();
       } catch (XmlPullParserException e) {
           e.printStackTrace();
       } catch(Exception e) {
           e.printStackTrace();
       }
       return str_result;
   }
   /**
    * 版本更新
    * @param VersionNo
    * @return
    */
   public static String[] UpdateMyself(String VersionNo)
   {
       String []str_result = new String[3];
       SoapObject request = new SoapObject(SERVER_NAMESPACE, FUC_UPDATEMYSELF);
       Log.d("guojie", "new SoapObject(SERVER_NAMESPACE,FUC_UPDATEMYSELF)");
       request.addProperty("VersionNo",VersionNo);
       Log.d("guojie"," VersionNo   =="+VersionNo );
       
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
               SoapEnvelope.VER11);
       envelope.bodyOut = request;
       envelope.dotNet = false;
       envelope.setOutputSoapObject(request);
       AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
       Log.d("guojie", "new AndroidHttpTransport(SERVER_URL)");
       ht.debug = true;
      
       Log.d("guojie", "envelope.setOutputSoapObject(request);");
       try {
           ht.call(SOAP_ACTION+FUC_UPDATEMYSELF, envelope);
           Log.d("guojie", "ht.call(null, envelope);");
           SoapObject result = (SoapObject) envelope.getResponse();
           Log.d("guojie", "(SoapObject) envelope.getResponse()");
           str_result[0] = result.getProperty("IsSuccess").toString();
           Log.d("guojie", "str_result == " + str_result);
           if(result.getProperty("ErrorMessage").toString()!=null) {
               str_result[1] =result.getProperty("ErrorMessage").toString();
           } else {
               str_result[1]="";
           }
           if(result.getProperty("DownLoadURL").toString()!=null) {
               str_result[2] =result.getProperty("DownLoadURL").toString();
               Log.d("guojie", "str_result[2] == " + str_result[2]);
           } else {
               str_result[2]="";
           }
       } catch (IOException e) {
           Log.e("guojie", "IOException error === " + e.toString());
           e.printStackTrace();
       } catch (XmlPullParserException e) {
           Log.e("guojie", "XmlPullParserException error === " + e.toString());
           e.printStackTrace();
       } catch(Exception e) {
           Log.e("guojie", "Exception error === " + e.toString());
           e.printStackTrace();
       }
       return str_result;
   }
    
    /**
    * 上传图片
    * @param IdCardNo
    * @param Name
    * @param picName
    * @param pics
    * @return
    */
    public static String UploadPicture(String IdCardNo,String Name,String picName ,byte [] pics ) {
        String str_result = null;
        SoapObject request = new SoapObject(SERVER_NAMESPACE,FUC_UPLOADPICTURE );
        request.addProperty("IdCardNo",IdCardNo );
        request.addProperty("Name",Name );
        request.addProperty("picName",picName );
        request.addProperty("picbytes",pics );
        Log.d("guojie", "picName ===="+picName);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = request;
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
        ht.debug = true;
        try {
            ht.call(SOAP_ACTION+FUC_UPLOADPICTURE, envelope);
//            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            Object  result = (Object) envelope.getResponse();
            String str_res = result.toString();
            Log.d("guojie", "envelope.getResponse() =="+result.toString());
            JSONObject rsJson = new JSONObject(str_res);
            
            str_result =rsJson.getString("IsSuccess") + "@";
            if(str_result.contains("true")) {
                
                str_result = str_result+"操作成功";
            } else {
                str_result = str_result
                +  rsJson.getString("ErrorMessage");
            }
            Log.d("guojie", "UploadPicture ===="+str_result);
        } catch (IOException e) {
            Log.d("guojie", "IOException ===="+e.toString());
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            Log.d("guojie", "XmlPullParserException ===="+e.toString());
            e.printStackTrace();
        } catch(Exception e) {
            Log.d("guojie", "Exception ===="+e.toString());
            e.printStackTrace();
        }
        return str_result;
    }
    
    /**
     * 
     * @param IdCardNo
     * @param Name
     * @param picName
     * @param pics
     * @param tag
     * @return
     */
    public static String UploadPictureTag(String IdCardNo,String Password,String picName ,String pics ,String tag) {
        String str_result = null;
        SoapObject request = new SoapObject(SERVER_NAMESPACE,FUC_UPLOADPICTURE );
        request.addProperty("IdCardNo",IdCardNo );
        request.addProperty("Password",Password  );
        request.addProperty("picName",picName );
        request.addProperty("pics",pics );
        request.addProperty("tag",tag );
        Log.d("guojie", "picName ===="+picName);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = request;
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
        ht.debug = true;
        try {
            ht.call(SOAP_ACTION+FUC_UPLOADPICTURE, envelope);
//            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            Object  result = (Object) envelope.getResponse();
            String str_res = result.toString();
            Log.d("guojie", "envelope.getResponse() =="+result.toString());
            JSONObject rsJson = new JSONObject(str_res);
            
            str_result =rsJson.getString("IsSuccess") + "@";
            if(str_result.contains("true")) {
                
                str_result = str_result+"操作成功";
            } else {
                str_result = str_result
                +  rsJson.getString("ErrorMessage");
            }
            Log.d("guojie", "UploadPicture ===="+str_result);
        } catch (IOException e) {
            Log.d("guojie", "IOException ===="+e.toString());
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            Log.d("guojie", "XmlPullParserException ===="+e.toString());
            e.printStackTrace();
        } catch(Exception e) {
            Log.d("guojie", "Exception ===="+e.toString());
            e.printStackTrace();
        }
        return str_result;
    }
    
    /**
     * 获得下载图片的链接
     * @return
     */
    public static String GetShowLink() {
        String str_result = null;
        SoapObject request = new SoapObject(SERVER_NAMESPACE,FUC_GETSHOWLINK);
        
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = request;
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
        ht.debug = true;
        try {
            ht.call(SOAP_ACTION+FUC_GETSHOWLINK, envelope);
//            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            Object  result = (Object) envelope.getResponse();
            String str_res = result.toString();
            Log.d("guojie", "envelope.getResponse() =="+result.toString());
            JSONObject rsJson = new JSONObject(str_res);
            
            str_result =rsJson.getString("IsSuccess") + "@";
            if(str_result.contains("true")) {
                
                str_result = str_result+"操作成功";
                picNum = Integer.valueOf(rsJson.getString("LinkNum"));
                PICURL = new String [picNum];
                LinkURL = new String [picNum];
                JSONArray picUrls = rsJson.getJSONArray("PicURL");
                JSONArray LinkUrls = rsJson.getJSONArray("LinkURL");
                for(int i = 0;i<picNum;i++) {
                    PICURL[i] = String.valueOf(picUrls.get(i));
                    LinkURL[i] = String.valueOf(LinkUrls.get(i));
                    Log.d("guojie", "PICURL[i]  ===  "+PICURL[i]);
                    Log.d("guojie", "LinkURL[i]  ===  "+LinkURL[i]);
                }
            } else {
                str_result = str_result
                        +  rsJson.getString("ErrorMessage");
            }
            Log.d("guojie", "UploadPicture ===="+str_result);
        } catch (IOException e) {
            Log.d("guojie", "IOException ===="+e.toString());
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            Log.d("guojie", "XmlPullParserException ===="+e.toString());
            e.printStackTrace();
        } catch(Exception e) {
            Log.d("guojie", "Exception ===="+e.toString());
            e.printStackTrace();
        }
        return str_result;
    }
    
    /**
     * 获得是否有新的健康信息
     * @param Identity
     * @param Password
     * @param LastCheckDate
     * @return
     */
    public static HashMap<String, Object> ExisitNewlyEHR(String Identity,String Password,String LastCheckDate ) {
        HashMap<String, Object> hmap = new HashMap<String, Object>();
        SoapObject request = new SoapObject(SERVER_NAMESPACE, FUC_EXISTNEWLYEHR);
        request.addProperty("Identity", Identity);
        request.addProperty("Password", Password);
        request.addProperty("LastCheckDate", LastCheckDate);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = request;
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        Log.d("guojie", "LastCheckDate in ExisitNewlyEHR====" +LastCheckDate);
        AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
        ht.debug = true;
        try {
            ht.call(SOAP_ACTION+FUC_EXISTNEWLYEHR, envelope);
            Log.d("guojie", "ht.call(null, envelope);");
//            SoapPrimitive  result = (SoapPrimitive) envelope.getResponse();
            Object  result = (Object) envelope.getResponse();
//            SoapObject  result = (SoapObject) envelope.getResponse();
            String str_res = result.toString();
            Log.d("guojie", "envelope.getResponse() =="+result.toString());
            JSONObject rsJson = new JSONObject(str_res);
            String IsSuccess = rsJson.getString("IsSuccess");
//            Log.d("guojie", "IsSuccess==="+IsSuccess);
            if(IsSuccess.contains("true")) {
                
                String PersonNum = rsJson.getString("PersonNum");
                Log.d("guojie", "PersonNum==="+PersonNum);
                String Name = rsJson.getString("Name");
                Log.d("guojie", "Name==="+Name);
                String Sex = rsJson.getString("Sex");
                Log.d("guojie", "Sex==="+Sex);
                String Age = rsJson.getString("Age");
                Log.d("guojie", "Age==="+Age);
                String IdCardNo = rsJson.getString("IdCardNo");
                Log.d("guojie", "IdCardNo==="+IdCardNo);
                String Address =rsJson.getString("Address");
                Log.d("guojie", "Address==="+Address);
                
                HashMap<String,Object> EHRecords = new HashMap<String, Object>();
                JSONArray ehrRecords = rsJson.getJSONArray("EHRRecords");
                for(int j = 0;j<ehrRecords.length();j++) {
                    JSONObject ehrRecord = ehrRecords.optJSONObject(j);
                    String CheckSN = ehrRecord.getString("CheckSN");
                    String CheckDate = ehrRecord.getString("CheckDate");
                    // LASTCHECKDATE = CheckDate;
                    String HospName = ehrRecord.getString("HospName");
                    JSONArray ehrDetails = ehrRecord.getJSONArray("EHRDetails");
                    HashMap<String,Object> EHRMap = new HashMap<String, Object>();
                    for(int i= 0;i<ehrDetails.length();i++) {
                        JSONObject ehrDetail = ehrDetails.optJSONObject(i);
                        HashMap<String, String> detail = new HashMap<String, String>();
                        String ItemSN = ehrDetail.getString("ItemSN");
                        String ItemCode = ehrDetail.getString("ItemCode");
                        String ItemName = ehrDetail.getString("ItemName");
                        String ItemNameAs = ehrDetail.getString("ItemNameAs");
                        String ItemValueDesc = ehrDetail.getString("ItemValueDesc");
                        String ItemUnit =ehrDetail.getString("ItemUnit");
                        String ClinicalMeaning =ehrDetail.getString("ClinicalMeaning");
                        String NormalValue1 = ehrDetail.getString("NormalValue1");
                        String NormalValue2 = ehrDetail.getString("NormalValue2");
                        String Tips = ehrDetail.getString("Tips");
                        String OrderBy = ehrDetail.getString("OrderBy");
                        detail.put("ItemSN", ItemSN);
                        detail.put("ItemCode", ItemCode);
                        detail.put("ItemName", ItemName);
                        detail.put("ItemNameAs", ItemNameAs);
                        detail.put("ItemValueDesc", ItemValueDesc);
                        detail.put("ItemUnit", ItemUnit);
                        detail.put("ClinicalMeaning", ClinicalMeaning);
                        detail.put("NormalValue1", NormalValue1);
                        detail.put("NormalValue2", NormalValue2);
                        detail.put("Tips", Tips);
                        detail.put("OrderBy", OrderBy);
                        EHRMap.put(ItemName, detail);
                    }
                    Log.d("guojie", "EHRlist==="+EHRMap.toString());
                    EHRecords.put("CheckSN", CheckSN);
                    EHRecords.put("CheckDate", CheckDate);
                    EHRecords.put("HospName", HospName);
                    EHRecords.put("EHRlist", EHRMap);
                }
                
                
                hmap.put("IsSuccess", IsSuccess);
                hmap.put("PersonNum", PersonNum);
                hmap.put("Name", Name);
                hmap.put("Sex", Sex);
                hmap.put("Age", Age);
                hmap.put("IdCardNo", IdCardNo);
                hmap.put("Address", Address);
                hmap.put(LASTCHECKDATE, EHRecords);
                Log.d("guojie", "IsSuccess==="+hmap.get("IsSuccess"));
                Log.d("guojie", "PersonNum==="+hmap.get("PersonNum"));
                Log.d("guojie", "Name==="+hmap.get("Name"));
                Log.d("guojie", "Age==="+hmap.get("Age"));
                Log.d("guojie", "Sex==="+hmap.get("Sex"));
                Log.d("guojie", "IdCardNo==="+hmap.get("IdCardNo"));
                Log.d("guojie", "Address==="+hmap.get("Address"));
                HashMap<String,Object> alist = (HashMap<String,Object>)hmap.get(LASTCHECKDATE);
                Log.d("guojie", "ItemSN==="+alist.get("CheckDate"));
                    HashMap<String, Object> details = (HashMap<String, Object>)alist.get("EHRlist");
                    HashMap<String, String> detail = (HashMap<String, String>)details.get("体温");
                    Log.d("guojie", "ItemSN==="+detail.get("ItemSN"));
                    Log.d("guojie", "ItemCode==="+detail.get("ItemCode"));
                    Log.d("guojie", "ItemName==="+detail.get("ItemName"));
                    Log.d("guojie", "ItemNameAs==="+detail.get("ItemNameAs"));
                    Log.d("guojie", "ItemValueDesc==="+detail.get("ItemValueDesc"));
                    Log.d("guojie", "ItemUnit==="+detail.get("ItemUnit"));
                    Log.d("guojie", "ClinicalMeaning==="+detail.get("ClinicalMeaning"));
                    Log.d("guojie", "NormalValue1==="+detail.get("NormalValue1"));
                    Log.d("guojie", "NormalValue2==="+detail.get("NormalValue2"));
                    Log.d("guojie", "Tips==="+detail.get("Tips"));
                    Log.d("guojie", "OrderBy==="+detail.get("OrderBy"));
                    detail = (HashMap<String, String>)details.get("体重");
                    if(detail!=null) {
                        Log.d("guojie", "detail==="+detail.toString());
                    }
                    Log.d("guojie", "@@@@@@@@@@@@@@Finish@@@@@@@@@@");
            }
            else {
                String ErrorMessage = rsJson.getString("ErrorMessage");
                hmap.put("IsSuccess", IsSuccess);
                hmap.put("ErrorMessage",ErrorMessage);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return hmap;
    }
    
    private static String CompareDate(String date) {
        String [] lastDate = LASTCHECKDATE.split("-");
        Log.d("guojie","LASTCHECKDATE in CompareDate ==="+LASTCHECKDATE);
        String [] newDate = date.split("-");
        Log.d("guojie", "return result in CompareDate " +(Integer.valueOf(newDate[1])>Integer.valueOf(lastDate[1])) );
//        Log.d("guojie", "new data ==" +Integer.valueOf(newDate[0])+"old date ==="+Integer.valueOf(lastDate[0]) );
        Log.d("guojie", "return new data in CompareDate ==" +Integer.valueOf(newDate[1])+"old date ==="+Integer.valueOf(lastDate[1]) );
//        Log.d("guojie", "new data ==" +Integer.valueOf(newDate[2])+"old date ==="+Integer.valueOf(lastDate[2]) );
        if(Integer.valueOf(newDate[0])>Integer.valueOf(lastDate[0])) {
            Log.d("guojie","return new data for year");
            return date;
        } else if((Integer.valueOf(newDate[1])>Integer.valueOf(lastDate[1]))&&(Integer.valueOf(newDate[0])==Integer.valueOf(lastDate[0]))) {
            Log.d("guojie","return new data for month");
            return date;
        } else if((Integer.valueOf(newDate[2])>Integer.valueOf(lastDate[2]))&&((Integer.valueOf(newDate[1])==Integer.valueOf(lastDate[1]))&&(Integer.valueOf(newDate[0])==Integer.valueOf(lastDate[0])))) {
            Log.d("guojie","return new data for day");
            return date;
        }
        Log.d("guojie","return LASTCHECKDATE in CompareDate=="+LASTCHECKDATE);
        return LASTCHECKDATE;
    }
    
    private static void sortDate() {
        String []dates  = new String[LIST_CHECKDATE.size()];
        LIST_CHECKDATE.toArray(dates);
        Arrays.sort(dates);
        LIST_CHECKDATE.clear();
        for(int i =0;i<dates.length;i++) {
            LIST_CHECKDATE.add(dates[dates.length-1-i]);
        }
    }
    
    /**
     * 获得体检项目
     * @return
     */
    public static String GetItems(String Identity,String Password) {
        String str_result = null;
        SoapObject request = new SoapObject(SERVER_NAMESPACE,FUC_GETITEMS);
        request.addProperty("Identity", Identity);
        request.addProperty("Password", Password);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        
        envelope.bodyOut = request;
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        AndroidHttpTransport ht = new AndroidHttpTransport(SERVER_URL);
        ht.debug = true;
        try {
            ht.call(SOAP_ACTION+FUC_GETITEMS, envelope);
//            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            Object  result = (Object) envelope.getResponse();
            String str_res = result.toString();
            Log.d("guojie", "envelope.getResponse() =="+result.toString());
            JSONObject rsJson = new JSONObject(str_res);
            
            str_result =rsJson.getString("IsSuccess") + "@";
            if(str_result.contains("true")) {
                
                str_result = str_result+"操作成功";
                JSONArray items = rsJson.getJSONArray("ItemName");
                EHRItems = new String[items.length()];
                for(int i = 0;i<items.length();i++) {
                    EHRItems[i] = String.valueOf(items.get(i));
                    Log.d("guojie", "EHRItems[i]  ===  "+EHRItems[i]);
                }
            } else {
                str_result = str_result
                        +  rsJson.getString("ErrorMessage");
            }
            Log.d("guojie", "UploadPicture ===="+str_result);
        } catch (IOException e) {
            Log.d("guojie", "IOException ===="+e.toString());
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            Log.d("guojie", "XmlPullParserException ===="+e.toString());
            e.printStackTrace();
        } catch(Exception e) {
            Log.d("guojie", "Exception ===="+e.toString());
            e.printStackTrace();
        }
        return str_result;
    }
}
